<?php
class Helper_Store{
    static $Domain;
    static function fromDomain($domain=null){
        self::$Domain=$domain=$_SERVER['HTTP_HOST'];
        if(strlen($domain)<=0) return false;
        if(substr($domain,0,4)=='apis'){ // apis , 域名
            return array(
                'store_id'=>'apis',
                'name'=>'apis',
                'domain'=>$domain,
            );
        }
        if(!isset($_SESSION['domain'][$domain])){
            $d=Store_Domain::find('domain=?',$domain)->getOne();
            if($d->isNewRecord()||$d->store->isNewRecord()){
                $_SESSION['domain'][$domain]=null;
            }else{
                $s=$d->store;
                $_SESSION['domain'][$domain]=array(
                    'store_id'=>$s->store_id,
                    'seller_id'=>$s->seller_id,
                    'name'=>$s->name,
                    'currency'=>$s->currency,
                    'domain'=>$domain,
                    'language'=>$s->language,
                    'layout'=>$s->layout
                );
            }
        }
        // Other config 
        return $_SESSION['domain'][$domain];
    }
    
    static function layout($layout_name=''){
        if(empty($layout_name)){
            $domain=$_SERVER['HTTP_HOST'];
            if(isset($_SESSION['domain'][$domain])){
                $layout_name=$_SESSION['domain'][$domain]['layout'];
            }
        }
        $path=Q::ini('app_config/APP_DIR').'/view/_layouts/';
        echo $path.$layout_name.'_layout.php';
        if(is_file($path.$layout_name.'_layout.php')){
            return '_layouts/'.$layout_name.'_layout';
        }else{ //default 
            return '_layouts/'.'ashop_layout';
        }
    }
    
    static function logo(){
        $path=Q::ini('app_config/ROOT_DIR').'/img/stores/';
        $web_path='img/stores/';
        $domain=$_SERVER['HTTP_HOST'];
        if(isset($_SESSION['domain'][$domain])){
            $store=$_SESSION['domain'][$domain];
        }
        $img_filename=$store['store_id'];
        $img_exfs=array(
            '.png','.gif','.jpg'
        );
        foreach($img_exfs as $exf){
            if(is_file($path.$img_filename.$exf)){
                return $web_path.$img_filename.$exf; break;
            }
        }
        return $web_path.'0.gif';
    }
}